package cn.wolfcode.luowowo.mgrsite.controller;

import cn.wolfcode.luowowo.article.domain.StrategyCommend;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.common.query.StrategyCommendQuery;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.mgrsite.util.UploadUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/strategyCommend")
public class StrategyCommendController {
    @Reference
    private IStrategyCommendService strategyCommendService;
    @Value("${file.dir}")
    private String dir;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") StrategyCommendQuery qo, Model model) {
        model.addAttribute("pageInfo",strategyCommendService.query(qo));
        return "strategyCommend/list";
    }

    @ResponseBody
    @RequestMapping("/save")
    public Object save(StrategyCommend entity, MultipartFile pic) {
        if (pic.getContentType().startsWith("image") && pic.getSize() > 0) {
            String coverUrl = UploadUtil.upload(pic, dir);
            entity.setCoverUrl("/" + coverUrl);
        }
        strategyCommendService.save(entity);
        return new AjaxResult();
    }


}
